<div class="app-content-body ">
    <div class="bg-light lter b-b wrapper-md hidden-print">
      <h1 class="m-n font-thin h3">更改合约</h1>
    </div>

    <div class="form_title">
      <div class="my_hd edit-type">编辑</div>
      <div class="crud-container crud-reset">
        <form id="form-container">
          <div class="row"> 
            <div class="col-sm-12">
              <div id="form"></div>
            </div>
          </div>          
          <div class="row">
            <div class="col-sm-12 text-right" style="margin-top: 15px;">
                <div id="cancel"></div>
                <div id="save"></div>
            </div>
          </div>
        </form>
      </div>
    </div>
</div>
<script type="text/javascript">
$(function (){
    $("#content").one("route.loaded", function(e, context) {
      var store = $.crudStore(API("subject-investor")), memberStore = $.crudStore(API('members')),
        id = context.params.id, formData = {};

      var formItems = [{
          dataField: "user.mobile",
          label: {text: '会员手机'},
          editorType: 'dxAutocomplete',
          validationRules: [{
	            type: "required",
	            message: "请输入会员手机号"
	     }],
          editorOptions: {
            dataSource: memberStore,
            valueExpr: "id",
            searchExpr: 'mobile',
            itemTemplate: function(data) {
                return data ? $("<div>" + (data.mobile || "") + "</div>") : null;
            },
            onItemClick: function(e) {
              var form = $("#form").dxForm("instance"), data = $.extend({}, form.option('formData'), 
              {user: e.itemData, quantity: e.itemData.subject_count});
              form.updateData(data);
            }
          }
        },{
          dataField: "user.username",
          label: {text: '会员姓名'}, 
          editorOptions: {readOnly: true}
      },{
          dataField: "user.identity_card",
          label: {text: '会员证件号'},
          editorOptions: {readOnly: true}
      },{
          dataField: "user.money_total",
          label: {text: '当前金额'},
          editorOptions: {readOnly: true},
      },{
          dataField: "user.last_static_time",
          label: {text: '当前剩余静泊时间'},
          editorOptions: {readOnly: true}
      },{
          dataField: "user.last_sea_time",
          label: {text: '当前剩余出海时间'},
          editorOptions: {readOnly: true}
      },{
          dataField: "offset_quantity",
          label: {text: context.params.action === 'add' ? "增持合约金额" : "减持合约金额"},
          editorType: 'dxNumberBox',
          editorOptions: {min: 1, showSpinButtons: true},
          validationRules: [{
              type: "required",
              message: "请输入" + (context.params.action === 'add' ? "增持合约金额" : "减持合约金额")
          }]
      },{
		  dataField: "static_time",
          label: {text: context.params.action === 'add' ? "增加静泊时间" : "减少静泊时间"},
          editorType: 'dxNumberBox',
          editorOptions: {showSpinButtons: true,min:0},
          validationRules: [{
              type: "required",
              message: "请输入" + (context.params.action === 'add' ? "增加静泊时间" : "减少静泊时间")
          }]
        },{
          dataField: "sea_time",
          label: {text: context.params.action === 'add' ? "增加出海时间" : "减少出海时间"},
          editorType: 'dxNumberBox',
          editorOptions: {showSpinButtons: true,min:0},
          validationRules: [{
              type: "required",
              message: "请输入" + (context.params.action === 'add' ? "增加出海时间" : "减少出海时间")
          }]
      }]
      formItems = formItems.concat([{
          dataField: "file",
          label: {text: '附件'},
          editorType: "dxFileUploader",
          editorOptions:{
            uploadMode: 'useForm',
            multiple: false,
            accept: "*",
            value: []
          }
      },{
          dataField: "remark",
          label: {text: '备注'},
          editorType: "dxTextArea",
          editorOptions: {
              height: 140
          }
      }]);

      var form = $("#form").dxForm({
        formData: {money: '', remark: '', quantity: 0, user: {}},
        showColonAfterLabel: true,
        showValidationSummary: false,
        validationGroup: "crud",
        items: formItems
      }).dxForm("instance");

//       if (id) {
//         console.log("load #" + id);
//         memberStore.byKey(id).then(function (resp) {
//           form.updateData({user: resp, quantity: resp.subject_count});
//         });
//       }

      $(".edit-type").text(context.params.action === 'add' ? '增持' : '减持');

      $("#form-container").on("submit", function(e) {
        var data = form.option('formData'), s = $.crudStore(API("rights-constract-history"));
        if (! data.user.id) {
          DevExpress.ui.dialog.alert('请指定合约对应的手机号', '提示');
          return false;
        }
        
//      $("#save").dxButton('option', 'disabled', true);

        var udata = {
          money: context.params.action === 'add' ? data.offset_quantity : - data.offset_quantity,
          static_time: context.params.action === 'add' ? data.static_time : - data.static_time,
          sea_time: context.params.action === 'add' ? data.sea_time : -data.sea_time,
          remark: data.remark,
          user_id: data.user.id,
          files : data.file
        };

        s.insert(udata).then(function (resp,values) {
	         if(!values.success){
	         	var _text = values.msg
	         	DevExpress.ui.dialog.alert(_text, '提示');
	            return false;
	         }else{
	           	 DevExpress.ui.notify({
			              message: "已完成",
			     }, "success", 1000);
		     		context.redirect("#/rightsConstractHistory");
		     }
	    }).fail(function () {
	          	DevExpress.ui.notify({
	          	message: $.crudStoreResp.msg,
         	}, "warning", 2000);
	          	$("#save").dxButton('option', 'disabled', false);
        });
        return false;
      });

      $("#save").dxButton({
        text: "保存",
        type: "success",
        useSubmitBehavior: true,
        validationGroup: "crud"
      });

      $("#cancel").dxButton({
          text: "取消",
          type: "normal",
          onClick:function() {
            context.redirect("#/rightsConstractHistory");
          }
      });
    });
});
</script>